package com.gzhryc.shared_device.common.device.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.common.device.dao.DeviceTypeFuncDao;
import com.gzhryc.shared_device.common.device.dao.db.DeviceTypeFunc;
import com.gzhryc.shared_device.common.device.services.dto.DeviceSearch;
import com.gzhryc.shared_device.common.device.services.dto.DeviceTypeFuncSearch;

public class DeviceTypeFuncService {

    static Logger log = Logger.getLogger(DeviceTypeFuncService.class);

    public static DeviceTypeFuncService self() {
        return MultiDBTools.getService(DeviceTypeFuncService.class, true);
    }

    public static DeviceTypeFuncService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, DeviceTypeFuncService.class, true);
    }

    DeviceTypeFuncDao dao;

    public DeviceTypeFuncService(String jdbcKey) {
        dao = new DeviceTypeFuncDao(jdbcKey);
    }

    public List<DeviceTypeFunc> findBySearch(DeviceTypeFuncSearch search, Integer pageNo, Integer pageLimit) {
        Conditions conditions = dao.buildConditions(search);
        conditions.desc("createDate");
        try {
            return dao.findByCondition(conditions, pageNo, pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public Long countBySearch(DeviceTypeFuncSearch search) {
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.count(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return 0L;
    }

    public List<DeviceTypeFunc> findByTypeId(String typeId){
        if(StringTools.isNotBlank(typeId)){
            Conditions conditions = new Conditions(DeviceTypeFunc.class);
            conditions.and().is("typeId",typeId);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<DeviceTypeFunc> findByCode(String code){
        if(StringTools.isNotBlank(code)){
            Conditions conditions = new Conditions(DeviceTypeFunc.class);
            conditions.and().is("code",code);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<String> findTypeIdByCode(String code){
        if(StringTools.isNotBlank(code)){
            Conditions conditions = new Conditions(DeviceTypeFunc.class);
            conditions.and().is("code",code);

            try {
                return dao.findTypeIdByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<DeviceTypeFunc> findByCodes(List<String> codes){
        if(codes != null && codes.size() > 0){
            Conditions conditions = new Conditions(DeviceTypeFunc.class);
            conditions.and().in("code",codes);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<DeviceTypeFunc> findByTypeIds(List<String> typeIds){
        if(typeIds != null && typeIds.size() > 0){
            Conditions conditions = new Conditions(DeviceTypeFunc.class);
            conditions.and().in("typeId",typeIds);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public DeviceTypeFunc getByKeys(String typeId, String code){
        if(StringTools.isNotBlank(typeId) && StringTools.isNotBlank(code)){
            Conditions conditions = new Conditions(DeviceTypeFunc.class);
            conditions.and().is("typeId",typeId);
            conditions.and().is("code",code);
            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public boolean add(DeviceTypeFunc deviceTypeFunc) throws LogicException {
        if(StringTools.isBlank(deviceTypeFunc.getProtocolType())){
            throw new LogicException("请填写通信协议");
        }
        deviceTypeFunc.setCreateDate(new Date());

        DeviceSearch deviceSearch = new DeviceSearch();
        deviceSearch.setDeviceTypeId(deviceTypeFunc.getTypeId());
        Long total = DeviceService.self().countBySearch(deviceSearch);

        JdbcSession.begin();
        try {
            if(dao.insert(deviceTypeFunc) > 0){
                if(total > 0){
                    if(DeviceService.self().addDeviceFunCode(deviceTypeFunc.getTypeId(),deviceTypeFunc)){
                        JdbcSession.commit();
                        return true;
                    }
                }else{
                    JdbcSession.commit();
                    return true;
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        } finally {
            JdbcSession.end();
        }
        return false;
    }

    public boolean updateNotNull(DeviceTypeFunc deviceTypeFunc) throws LogicException {
        if(deviceTypeFunc != null && StringTools.isNotBlank(deviceTypeFunc.getTypeId()) && StringTools.isNotBlank(deviceTypeFunc.getCode())) {
            Conditions conditions = new Conditions(DeviceTypeFunc.class);
            conditions.and().is("typeId", deviceTypeFunc.getTypeId());
            conditions.and().is("code", deviceTypeFunc.getCode());

            try {
                return dao.updateNotNull(deviceTypeFunc,conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean deleteByKeys(String typeId,String code) throws LogicException{
        if(StringTools.isNotBlank(typeId) && StringTools.isNotBlank(code)){
            DeviceSearch deviceSearch = new DeviceSearch();
            deviceSearch.setDeviceTypeId(typeId);
            Long total = DeviceService.self().countBySearch(deviceSearch);

            Conditions conditions = new Conditions(DeviceTypeFunc.class);
            conditions.and().is("typeId",typeId);
            conditions.and().is("code",code);

            JdbcSession.begin();
            try {
                if(dao.deleteByCondition(conditions) > 0){
                    if(total > 0){
                        if(DeviceService.self().delDeviceFunCode(typeId,code)){
                            JdbcSession.commit();
                            return true;
                        }
                    }else{
                        JdbcSession.commit();
                        return true;
                    }
                }
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }finally {
                JdbcSession.end();
            }
        }
        return false;
    }

    public boolean deleteByTypeId(String typeId){
        if(StringTools.isNotBlank(typeId)){
            Conditions conditions = new Conditions(DeviceTypeFunc.class);
            conditions.and().is("typeId",typeId);

            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }
}
